package com.zhanghp.class006;

import java.util.Arrays;

import static com.zhanghp.common.ArrUtil.validator;

/**
 * @author zhanghp
 * @since 2024/3/4 18:02
 */
// 有序数组中是否存在一个数字
public class Code01_FindeNumber {

    public static void main(String[] args) {
        validator(1_000, 10, 100, arr -> {
            Arrays.sort(arr);
            int num = (int) (Math.random() * 100) + 1;
            if (right(arr, num) != exist(arr, num)) {
                System.out.println(Arrays.toString(arr) + "  " + num);
                System.out.println("出错了！");
            }
        });
    }


    public static boolean right(int[] arr, int num) {
        if (arr == null || arr.length == 0) {
            return false;
        }
        for (int i : arr) {
            if (i == num) {
                return true;
            }
        }
        return false;
    }

    public static boolean exist(int[] arr, int num) {
        if (arr == null || arr.length == 0) {
            return false;
        }
        int l = 0, r = arr.length - 1, mid = 0;
        while (l <= r) {
            mid = l + ((r - l) >> 1);
            if (arr[mid] == num) {
                return true;
            } else if (num >= arr[mid]) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return false;
    }
}
